<template>
  <view class="main">
    <!-- 购物车商品展示 -->
    <view class="goodsShow">
      <view class="allGoods">
        全部商品
      </view>
    </view>
    <!-- 商品展示处 多选 -->
    <view class="goodsList">
      <uni-swipe-action>
        <block v-for="(goods,index) in cart" :key="index">
          <uni-swipe-action-item :right-options="Newoptions" @click="deleteGoods(goods)">
            <view class="goods-item">
              <!-- 商品左侧图片区域 -->
              <view class="goods-item-left">
                <image :src="imgPrefixAddress + goods.goodsPreviews.split(',')[0]" class="goods-pic"></image>
              </view>
              <!-- 商品右侧信息区域 -->
              <view class="goods-item-right">
                <!-- 商品标题 -->
                <view class="goods-name">{{goods.goodsName}}</view>
                <view class="goods-specification">
                  {{goods.name}}
                </view>
                <view class="goods-info-box">
                  <!-- 商品价格 -->
                  <view class="goods-price">￥{{goods.retailValue}}</view>
                  <!-- 商品数量 -->
                  <uni-number-box :min="1" :value="goods.amount"
                    @change="numChangeHandler($event,goods.cartId)"></uni-number-box>
                </view>
              </view>
            </view>
          </uni-swipe-action-item>
        </block>
      </uni-swipe-action>
    </view>
    <!-- 购物车为空 -->
    <u-empty mode="car" icon="http://cdn.uviewui.com/uview/empty/car.png" :show="(cart.length == 0)">
    </u-empty>
    <!-- 猜你喜欢 -->
    <view class="guessYouLike">
      猜你喜欢
    </view>
    <view class="guessYouLikeList">
      <navigator class="item" v-for="(item,index) in guessYouLike" :key="index"
        :url="'/subpkg/goods_detail/goods_detail?goodsId=' + item.goodsId">
        <image :src="imgPrefixAddress + item.goodsPreviews.split(',')[0]"></image>
        <view class="title">
          {{item.goodsName}}
        </view>
        <view class="price">
          ¥{{item.retailValue}}
        </view>
      </navigator>
    </view>
    <!-- 全选结算 -->
    <view class="jiesuan" @click="goJiesuan">

      结算

    </view>
    <!-- 规格底部弹出层 -->
    <u-popup :show="show" @close="close">
      <view class="specification-bottom">
        <!-- 图片标题上面 -->
        <view class="specification-bottom-pic">
          <view class="specification-bottom-pic-left">
            <image src="@/static/bottom.jpg"></image>
          </view>
          <view class="specification-bottom-pic-right">
            <view class="price">
              ¥2388.00
            </view>
            <view class="goods-number">
              商品编号：KFDGH-SKHF -KSDSDH...
            </view>
          </view>
        </view>
        <u-line length="50%"></u-line>
        <!-- 规格1 -->
        <view class="specification-bottom-one"></view>
        <!-- 规格2 -->
        <view class="specification-bottom-two"></view>
      </view>
    </u-popup>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        imgPrefixAddress: "https://bjlr-1316909348.cos.ap-chengdu.myqcloud.com",
        Newoptions: [{
          text: '删除',
          style: {
            backgroundColor: '#B8C1CC'
          }
        }],
        // 规格底部弹出层
        show: false,
        cart: [],
        guessYouLike: []
      };
    },
    methods: {
      //去结算页面
      goJiesuan() {
        let that = this
        if (this.cart == '') return uni.$showMsg('请先加入购物车')
        uni.navigateTo({
          url: '/subpkg/buyNow/buyNow',
          success(res) {
            res.eventChannel.emit('acceptDataFromOpenerPage', that.cart)
          }
        })
      },
      //删除商品
      async deleteGoods(goods) {
        const params = {
          cartId: goods.cartId
        }
        const res = await uni.$http.post('/flow/cart/remove', params)
        if (res.data.code !== 200) return uni.$showMsg('删除失败')
        this.getUserCarCount()
      },
      //加减商品数量
      numChangeHandler(value, id) {
        let obj = {
          cartId: id + '',
          amount: value + ''
        }
        this.updateBuyCarCount(obj)
      },
      // updateBuyCarCount点击修改购物车数量
      async updateBuyCarCount(obj) {
        const res = await uni.$http.post('/flow/cart/quantitychange', obj)
        if (res.data.code !== 200) return uni.$showMsg('修改失败')
        this.getUserCarCount()
      },
      // 规格底部弹出层
      close() {
        this.show = false
        // console.log('close');
      },
      // 获取当前用户购物车数据
      async getUserCarCount() {
        let res = await uni.$http.get('/flow/cart/list')
        this.cart = res.data.data
      },
      async getGuessYouLike() {
        const res = await uni.$http.get('/flow/cart/random')
        this.guessYouLike = res.data.data
      }
    },
    onLoad() {
      //获取当前用户购物车数据
      this.getUserCarCount()
      // 获取猜你喜欢的内容
      this.getGuessYouLike()
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #F5F6F9 !important;
  }

  .main {
    padding-bottom: 40rpx;
  }

  .goodsShow {
    height: 50px;
    display: flex;
    justify-content: space-between;
    line-height: 50px;
    font-size: 14px;
    color: #141414;
    padding: 0 20rpx;
    background-color: #fff;
  }

  // 商品item
  .goodsList {
    .goods-item {
      display: flex;
      padding: 10px 5px;
      border-bottom: 1px solid #f0f0f0;

      .goods-item-left {
        margin-right: 5px;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .goods-pic {
          width: 100px;
          height: 100px;
          display: block;
        }
      }

      .goods-item-right {
        display: flex;
        flex: 1;
        flex-direction: column;
        justify-content: space-between;

        .goods-info-box {
          display: flex;
          justify-content: space-between;
          align-items: center;
        }

        .goods-name {
          font-size: 14px;
          color: #66737F;
        }

        .goods-specification {
          font-size: 10px;
          color: #8F9CA9;
        }

        .goods-price {
          font-size: 14px;
          color: #141414;
        }
      }
    }
  }

  .desc {
    background-color: #D5DDE5;
    font-size: 10px;
  }

  .title {
    font-size: 14px;
    color: #66737F;
  }

  .custom {
    margin-top: 20px;
  }

  // 猜你喜欢
  .guessYouLike {
    width: 100%;
    text-align: center;
    margin-top: 40rpx;
    font-size: 18px;
    color: #3D3D3D;
    margin-bottom: 30rpx;
  }

  // .right-delete {
  //   background-color: black;
  //   width: 100%;
  //   height: 100%;
  //   line-height: 134px;
  //   color: white;
  // }

  .guessYouLikeList {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-evenly;

    .item {
      width: 165px;
      height: 210px;

      image {
        width: 100%;
        height: 131px;
      }
    }
  }

  // 全选结算
  .settlement {
    width: 100%;
    height: 100rpx;
    background-color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    bottom: calc(var(--window-bottom));
    z-index: 1030;



    .one {
      .radio {
        font-size: 12px;
      }
    }

    .two {
      text-align: center;
      line-height: 100rpx;

      span {
        color: #A50000;
        font-size: 24px;
      }
    }

    .settlementButton {
      color: white;
      background-color: #141414;
      width: 90px;
      height: 36px;
      border-top-right-radius: 17px;
      border-bottom-right-radius: 17px;
      line-height: 36px;
      text-align: center;
    }
  }

  // 规格底部弹出层
  .specification-bottom {
    height: 370px;
    padding: 0 30rpx;

    .specification-bottom-pic {
      position: relative;

      .specification-bottom-pic-left {
        width: 120px;
        height: 96px;
        position: absolute;
        top: -48px;

        image {
          width: 100%;
          height: 100%;
        }
      }

      .specification-bottom-pic-right {
        margin-left: 270rpx;

        .price {
          color: #B02020;
          font-size: 24px;
        }

        .goods-number {
          font-size: 11px;
          color: #66737F;
          white-space: nowrap;
          text-overflow: ellipsis;
        }
      }
    }
  }

  .jiesuan {
    margin: 0 auto;
    width: 300px;
    height: 40px;
    border-radius: 10px;
    background-color: #000000;
    color: white;
    line-height: 40px;
    text-align: center;
  }
</style>